package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.lang.Snowflake;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.domain.Emplyee;
import com.ruoyi.system.domain.Leave;
import com.ruoyi.system.domain.RepairDetail;
import com.ruoyi.system.dto.ChangeStatusDto;
import com.ruoyi.system.dto.RepairDto;
import com.ruoyi.system.service.IEmplyeeService;
import com.ruoyi.system.service.IRepairDetailService;
import com.ruoyi.system.utils.IdGeneratorSnowflake;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.system.domain.Repair;
import com.ruoyi.system.service.IRepairService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 维修管理表Controller
 *
 * @author ruoyi
 * @date 2023-11-03
 */
@RestController
@RequestMapping("/repair")
public class RepairController extends BaseController
{
    @Autowired
    private IRepairService repairService;
    @Autowired
    private IRepairDetailService repairDetailService;
    @Autowired
    private IEmplyeeService emplyeeService;
    @Autowired
    private IdGeneratorSnowflake idGeneratorSnowflake;

    /**
     * 查询维修管理表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Repair repair)
    {
        startPage();
        List<Repair> list = repairService.selectRepairList(repair);
        return getDataTable(list);
    }

    /**
     * 导出维修管理表列表
     */
    @Log(title = "维修管理表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Repair repair)
    {
        List<Repair> list = repairService.selectRepairList(repair);
        ExcelUtil<Repair> util = new ExcelUtil<Repair>(Repair.class);
        util.exportExcel(response, list, "维修管理表数据");
    }

    /**
     * 获取维修管理表详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        System.out.println("维修id:"+id);
        return success(repairService.selectRepairById(id));
    }

    /**
     * 新增维修管理表
     */
    @Log(title = "维修管理表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RepairDto dto)
    {
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();
        System.out.println("sysUser:"+sysUser);
        Emplyee emplyee = emplyeeService.selectEmplyeeByEno(sysUser.getEno());
        System.out.println("当前申请维修员工："+emplyee.getName());
        Repair repair = new Repair();
        BeanUtils.copyProperties(dto,repair);
        repair.setId(idGeneratorSnowflake.snowflakeId());
        repair.setRepairApplicant(emplyee.getName());
        repair.setRepairAppdate(new Date());
        repair.setStatus("1");
        repair.setRno(idGeneratorSnowflake.snowflakeId()+"");
        //插入维修详情
        RepairDetail repairDetail = new RepairDetail();
        BeanUtils.copyProperties(dto,repairDetail);
        repairDetail.setRepairId(repair.getId());
        repairDetailService.insertRepairDetail(repairDetail);
        return toAjax(repairService.insertRepair(repair));
    }

    /**
     * 修改维修管理表
     */
    @Log(title = "维修管理表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Repair repair)
    {
        return toAjax(repairService.updateRepair(repair));
    }

    /**
     * 修改审批状态
     */
    @PutMapping("/changeStatus")
    public AjaxResult changeStatus(@RequestBody ChangeStatusDto dto)
    {
        Repair repair = new Repair();
        repair.setId(dto.getId());
        repair.setStatus(dto.getStatus().toString());
        return toAjax(repairService.updateRepair(repair));
    }

    /**
     * 删除维修管理表
     */
    @Log(title = "维修管理表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(repairService.deleteRepairByIds(ids));
    }
}
